home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 September (IDG) / Sep99.iso / Shareware World / Utilities / Text Processing / Alpha / Tcl / Modes / latex Mode / latexNavigation.tcl < prev    next >
Encoding:
Text File  |  1999-01-28  |  10.7 KB  |  354 lines  |  [TEXT/ALFA]

  1. #############################################################################
  2. #############################################################################
  3. #
  4. # latexNavigation.tcl (sourced on demand)
  5. #
  6. #############################################################################
  7. #
  8. # Author:  Tom Scavo <trscavo@syr.edu>
  9. # Updates 98-99, Vince Darley
  10. #############################################################################
  11. #############################################################################
  12.  
  13. # Find the next or previous environment.  Search forward (or backward, 
  14. # depending on $forward) for either \begin or \end.  If \begin is found, 
  15. # search forward for corresponding \end; otherwise, search backward for 
  16. # corresponding \begin.  Select the found environment, or display an 
  17. # error message if no environment is found.
  18. proc findEnvironment {forward} {
  19.     set searchString1 {^[ \t]*\\begin\{[^\{\}]*\}|\\end\{[^\{\}]*\}[ \t]*\r?}
  20.     set searchPos [getPos]
  21.     if { [isSelection] } { 
  22.     if { $forward } {
  23.         set searchPos [selEnd]
  24.     } else {
  25.         set searchPos [pos::math $searchPos - 1]
  26.     }
  27.     } else {
  28.     if { $forward } {
  29.         set searchPos [pos::math $searchPos + 1]
  30.     } else {
  31.         set searchPos [pos::math $searchPos - 1]
  32.     }
  33.     }
  34.     set searchResult [search -s -f $forward -r 1 -n $searchString1 $searchPos]
  35.     if {[string length $searchResult]} {
  36.     set begPos [lindex $searchResult 0]
  37.     set endPos [lindex $searchResult 1]
  38.     set searchText [getText $begPos $endPos]
  39.     regexp {\{(.*)\}} $searchText dummy envName
  40.     if {[string match {*begin*} $searchText]} {
  41.         set begEnv $begPos
  42.         append searchString2 {\\end\{} $envName {\}[ \t]*\r?}
  43.         set searchPos $endPos
  44.         set searchResult [search -s -f 1 -r 1 -n $searchString2 $searchPos]
  45.         if {[string length $searchResult]} {
  46.         set endPos [lindex $searchResult 1]
  47.         return [list $begPos $endPos]
  48.         } else {
  49.         return "matching \\end not found"
  50.         }
  51.     } else {
  52.         set endEnv $endPos
  53.         append searchString2 {^[ \t]*\\begin\{} $envName {\}}
  54.         set searchPos $begPos
  55.         set searchResult [search -s -f 0 -r 1 -n $searchString2 $searchPos]
  56.         if {[string length $searchResult]} {
  57.         set begPos [lindex $searchResult 0]
  58.         return [list $begPos $endPos]
  59.         } else {
  60.         return "matching \\begin not found"
  61.         }
  62.     }
  63.     } else {
  64.     if { $forward } {
  65.         return "next environment not found"
  66.     } else {
  67.         return "previous environment not found"
  68.     }
  69.     }
  70. }
  71.  
  72. proc prevEnvironment {} {
  73.     global searchNoisily
  74.     set findResult [findEnvironment 0]
  75.     if {[llength $findResult] == 2} {
  76.         goto [lindex $findResult 0]
  77.     } else {
  78.         if {$searchNoisily} {beep}
  79.         message $findResult
  80.     }
  81. }
  82. proc nextEnvironment {} {
  83.     global searchNoisily
  84.     set findResult [findEnvironment 1]
  85.     if {[llength $findResult] == 2} {
  86.         goto [lindex $findResult 0]
  87.     } else {
  88.         if {$searchNoisily} {beep}
  89.         message $findResult
  90.     }
  91. }
  92. proc prevEnvironmentSelect {} {
  93.     global searchNoisily
  94.     set forward 0
  95.     set findResult [findEnvironment $forward]
  96.     if {[llength $findResult] == 2} {
  97.         set endPos [lindex $findResult 1]
  98. #         if { [regexp {\r} [lookAt [expr $endPos + 1]]] } {
  99. #             set endPos [expr $endPos + 1]
  100. #         }
  101.         select [lindex $findResult 0] $endPos
  102.     } else {
  103.         if { $searchNoisily } {beep}
  104.         message $findResult
  105.     }
  106. }
  107. proc nextEnvironmentSelect {} {
  108.     global searchNoisily
  109.     set forward 1
  110.     set findResult [findEnvironment $forward]
  111.     if {[llength $findResult] == 2} {
  112.         set endPos [lindex $findResult 1]
  113. #         if { [regexp {\r} [lookAt [expr $endPos + 1]]] } {
  114. #             set endPos [expr $endPos + 1]
  115. #         }
  116.         select [lindex $findResult 0] $endPos
  117.     } else {
  118.         if { $searchNoisily } {beep}
  119.         message $findResult
  120.     }
  121. }
  122.  
  123. # Find a LaTeX command in either direction.  It's up to the calling
  124. # procedure to pass the correct starting position of the search.
  125. proc findCommand {pos direction} {
  126.     #    Handle "\ " and "\[" separately:
  127.     set searchString {(\\([^a-zA-Z\t\r* []|[a-zA-Z]+)\*?)|([^\\]\\\ )|([^\\]\\\[)}
  128.     set searchResult [search -s -f $direction -r 1 -n $searchString $pos]
  129.     if { [string length $searchResult] } {
  130.     set begPos [lindex $searchResult 0]
  131.     set endPos [lindex $searchResult 1]
  132.     set lastChar [lookAt [pos::math $endPos - 1]]
  133.     if {$lastChar == "\ " || $lastChar == "\["} {
  134.         return [list [pos::math $begPos + 1] $endPos]
  135.     } else {}
  136.     }
  137.     return $searchResult
  138.     # Handles everything but "\ " and "\[":
  139.     #     set searchString {\\([^a-zA-Z\t\r* []|[a-zA-Z]+)\*?}
  140.     #     return [search -s -f $direction -r 1 -n $searchString $pos]
  141. }
  142. # Find and goto the beginning of the previous LaTeX command.
  143. proc prevCommand {} {
  144.     TeX::prev Command
  145. }
  146. # Find and goto the beginning of the next LaTeX command.
  147. proc nextCommand {} {
  148.     TeX::next Command
  149. }
  150. # Select the previous LaTeX command, but do not attempt to select
  151. # any associated argument.
  152. proc prevCommandSelect {} {
  153.     TeX::prev Command 1
  154. }
  155. # Select the next LaTeX command, but do not attempt to select
  156. # any associated argument.
  157. proc nextCommandSelect {} {
  158.     TeX::next Command 1
  159. }
  160.  
  161. # Find a LaTeX command with arguments in either direction.  It's up 
  162. # to the calling procedure to pass the starting position of the search.
  163. # (Handles everything but "\ " and commands whose arguments contain
  164. # embedded braces.)
  165. proc findCommandWithArgs {pos direction} {
  166.     set searchString {\\([^a-zA-Z\t\r]|[a-zA-Z]+\*?)(\[.*\])*({[^{}]*})*}
  167.     return [search -s -f $direction -r 1 -n $searchString $pos]
  168. }
  169. # Select the previous LaTeX command and any associated arguments.
  170. proc prevCommandSelectWithArgs {} {
  171.     TeX::prev CommandWithArgs 1 command
  172. }
  173. # Select the next LaTeX command and any associated arguments.
  174. proc nextCommandSelectWithArgs {} {
  175.     TeX::next CommandWithArgs 1 command
  176. }
  177.  
  178. # Find a LaTeX sectioning command in either direction.  It's up to 
  179. # the calling procedure to pass the starting position of the search.
  180. proc findSection {pos direction} {
  181.     global funcExprAlt
  182.     return [search -s -f $direction -r 1 -n $funcExprAlt $pos]
  183. }
  184. # Select the previous LaTeX sectioning command.
  185. proc prevSection {} {
  186.     TeX::prev Section
  187. }
  188. # Select the next LaTeX sectioning command.
  189. proc nextSection {} {
  190.     TeX::next Section
  191. }
  192. proc prevSectionSelect {} {
  193.     global searchNoisily
  194.     set pos [getPos]
  195.     if {[pos::compare $pos > [minPos]]} {
  196.     set searchResult [findSection [pos::math $pos - 1] 0]
  197.     if { [string length $searchResult] } {
  198.         set begPos [lindex $searchResult 0]
  199.         set searchPos [pos::math [lindex $searchResult 1] + 1]
  200.         set searchResult [findSection $searchPos 1]
  201.         if { [string length $searchResult] } {
  202.         set endPos [lindex $searchResult 0]
  203.         } else {
  204.         set searchResult [search -s -f 1 -r 0 -n "\\end{document}" $searchPos]
  205.         set endPos [lindex $searchResult 0]
  206.         }
  207.         select $begPos $endPos
  208.         return
  209.     }
  210.     }
  211.     if { $searchNoisily } {beep}
  212.     message {previous section not found}
  213. }
  214. # Select the next LaTeX sectioning command.
  215. proc nextSectionSelect {} {
  216.     global searchNoisily
  217.     set pos [getPos]
  218.     if {$pos < [maxPos]} {
  219.     if { [isSelection] } {
  220.         set pos [pos::math $pos + 1]
  221.     }
  222.     set searchResult [findSection $pos 1]
  223.     if { [string length $searchResult] } {
  224.         set begPos [lindex $searchResult 0]
  225.         set searchPos [pos::math [lindex $searchResult 1] + 1]
  226.         set searchResult [findSection $searchPos 1]
  227.         if { [string length $searchResult] } {
  228.         set endPos [lindex $searchResult 0]
  229.         } else {
  230.         set searchResult [search -s -f 1 -r 0 -n "\\end{document}" $searchPos]
  231.         set endPos [lindex $searchResult 0]
  232.         }
  233.         select $begPos $endPos
  234.         return
  235.     }
  236.     }
  237.     if { $searchNoisily } {beep}
  238.     message {next section not found}
  239. }
  240.  
  241. # Find a LaTeX subsectioning command in either direction.  It's up to 
  242. # the calling procedure to pass the starting position of the search.
  243. proc findSubsection {pos direction} {
  244.     global funcExpr
  245.     return [search -s -f $direction -r 1 -n $funcExpr $pos]
  246. }
  247. proc prevSubsection {} {
  248.     TeX::prev Subsection
  249. }
  250. # Select the next LaTeX sectioning command.
  251. proc nextSubsection {} {
  252.     TeX::next Subsection
  253. }
  254. proc prevSubsectionSelect {} {
  255.     global searchNoisily
  256.     set pos [getPos]
  257.     if {[pos::compare $pos > [minPos]]} {
  258.     set searchResult [findSubsection [pos::math $pos - 1] 0]
  259.     if { [string length $searchResult] } {
  260.         set begPos [lindex $searchResult 0]
  261.         set endPos [lindex $searchResult 1]
  262.         set searchPos [pos::math $endPos + 1]
  263.         set commandName [extractCommandName [getText $begPos $endPos]]
  264.         if {[string match {section*} $commandName]} {
  265.         set searchResult [findSection $searchPos 1]
  266.         } else {
  267.         set searchResult [findSubsection $searchPos 1]
  268.         }
  269.         if { [string length $searchResult] } {
  270.         set endPos [lindex $searchResult 0]
  271.         } else {
  272.         set searchResult [search -s -f 1 -r 0 -n "\\end{document}" $searchPos]
  273.         set endPos [lindex $searchResult 0]
  274.         }
  275.         select $begPos $endPos
  276.         return
  277.     } 
  278.     }
  279.     if { $searchNoisily } {beep}
  280.     message {previous (sub)*section not found}
  281. }
  282. # Select the next LaTeX sectioning command.
  283. proc nextSubsectionSelect {} {
  284.     global searchNoisily
  285.     set pos [getPos]
  286.     if {[pos::compare $pos < [maxPos]]} {
  287.     if { [isSelection] } {
  288.         set pos [pos::math $pos + 1]
  289.     }
  290.     set searchResult [findSubsection $pos 1]
  291.     if { [string length $searchResult] } {
  292.         set begPos [lindex $searchResult 0]
  293.         set endPos [lindex $searchResult 1]
  294.         set searchPos [pos::math $endPos + 1]
  295.         set commandName [extractCommandName [getText $begPos $endPos]]
  296.         if {[string match {section*} $commandName]} {
  297.         set searchResult [findSection $searchPos 1]
  298.         } else {
  299.         set searchResult [findSubsection $searchPos 1]
  300.         }
  301.         if { [string length $searchResult] } {
  302.         set endPos [lindex $searchResult 0]
  303.         } else {
  304.         set searchResult [search -s -f 1 -r 0 -n "\\end{document}" $searchPos]
  305.         set endPos [lindex $searchResult 0]
  306.         }
  307.         select $begPos $endPos
  308.         return
  309.     } 
  310.     }
  311.     if { $searchNoisily } {beep}
  312.     message {next (sub)*section not found}
  313. }
  314.  
  315. proc TeX::prev {what {select 0} {msg ""}} {
  316.     global searchNoisily
  317.     set pos [getPos]
  318.     if {[pos::compare $pos > [minPos]]} {
  319.     set searchResult [find$what [pos::math $pos - 1] 0]
  320.     if { [string length $searchResult] } {
  321.         if {$select} {
  322.         eval select $searchResult
  323.         } else {
  324.         goto [lindex $searchResult 0]
  325.         }
  326.         return
  327.     } 
  328.     }
  329.     if { $searchNoisily } {beep}
  330.     if {$msg == ""} {set msg [string tolower $what]}
  331.     message "previous $msg not found"
  332. }
  333. proc TeX::next {what {select 0} {msg ""}} {
  334.     global searchNoisily
  335.     set pos [getPos]
  336.     if {[pos::compare $pos < [maxPos]]} {
  337.     if { [isSelection] } {
  338.         set pos [pos::math $pos + 1]
  339.     }
  340.     set searchResult [find$what [pos::math $pos + 1] 1]
  341.     if { [string length $searchResult] } {
  342.         if {$select} {
  343.         eval select $searchResult
  344.         } else {
  345.         goto [lindex $searchResult 0]
  346.         }
  347.         return
  348.     } 
  349.     }
  350.     if { $searchNoisily } {beep}
  351.     if {$msg == ""} {set msg [string tolower $what]}
  352.     message "next $msg not found"
  353. }
  354.